struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* cur = head;
    struct ListNode* prev = NULL;
    while (cur != NULL) {
        if (cur->val == val) {
            if (prev != NULL) {
                prev->next = cur->next;
                free(cur);
                cur = prev->next;
            }
            else {
                cur = cur->next;
                free(head);
                head = cur;
            }
        }
        else {
            prev = cur;
            cur = cur->next;
        }
    }

    return head;
}